package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"updown-sys/models"
)

func Add(c *gin.Context) {
	name := c.PostForm("name")
	up_num, _ := strconv.Atoi(c.PostForm("up_num"))
	types := c.PostForm("types")

	if name == "" || up_num == 0 || types == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}

	data := models.Good{
		Name:  name,
		UpNum: up_num,
		Types: types,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return

}

func List(c *gin.Context) {
	var good []models.Good
	models.DB.Find(&good)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": good,
	})
	return
}
